<script setup>
import { pageApi } from '../../api/user';
import { ref,onMounted } from 'vue';
import {
  Check,
  Delete,
  Edit,
  Message,
  Search,
  Star,
} from '@element-plus/icons-vue'

// 定义事件对象
const emits = defineEmits(['update','add','delete'])
// 搜索框导入
const search = ref({
   name:'',
  gender:''
})
// 表格导入
const tableData = ref([])
// 分页属性配置
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)
// 分页方法
const handleSizeChange = (val)=>{
pageSize.value = val
handleSearch()
}
const handleCurrentChange = (val)=>{
  currentPage.value =val
  handleSearch()
}
// 查询配置
const handleSearch =async()=>{
  let result = await pageApi(search.value.name,search.value.gender,currentPage.value,pageSize.value)
  tableData.value = result.data.rows
  total.value = result.data.total
}
const clean =async()=>{
  search.value={
    name:'',
    gender:''
  }
  handleSearch()
}
// 钩子函数
onMounted(()=>{
  handleSearch()
})
// 修改
const handleEdit = (row) =>{  
    emits('update',row)
}
// 增加
const handleAdd = () =>{
  emits('add')
}
// 删除
const handleDelete = (id) =>{
  emits('delete',id)
}
// 暴露方法
defineExpose({
handleSearch,
tableData
})

</script>
<template>
 <!-- 搜索框 -->
  <el-form :model="search" label-width="auto" style="max-width: auto">
    <el-row :gutter="10">
      <el-col :span="8">
      <el-form-item label="姓名">
        <el-input v-model="search.name" />
      </el-form-item>
      </el-col>
       <el-col :span="8">
      <el-form-item label="性别">
        <el-select v-model="search.gender" placeholder="性别">
          <el-option label="男" value="1" />
          <el-option label="女" value="2" />
        </el-select>
      </el-form-item>
       </el-col>
       <el-col :span="8">
         <el-button type="primary" @click="handleSearch">查询</el-button>
      <el-button type="success" @click="clean">清空</el-button>
       </el-col>

    </el-row>
  </el-form>
 
  <br>
  <!-- 新增 -->
    <el-button type="primary" @click="handleAdd">新增</el-button>
  <br>
   <br>
    <br>
<!-- 表格 -->
 <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="id" label="员工编号" />
    <el-table-column prop="name" label="姓名" width="180" />
    <el-table-column prop="username" label="用户名" />
    <el-table-column prop="gender" label="性别" width="180" >
      <template #default="scope">
        {{ scope.row.gender==1?'男':'女' }}
      </template>
    </el-table-column>
    <el-table-column prop="phone" label="电话" />
    <el-table-column prop="createTime" label="创建时间" />
    <el-table-column prop="updateTime" label="修改时间" />
 <el-table-column fixed="right" label="操作" min-width="120">
      <template #default="scope">
        <el-button type="primary" @click="handleEdit(scope.row)" :icon="Edit" circle />
        <el-button type="danger"  @click="handleDelete(scope.row)" :icon="Delete" circle />
      </template>
    </el-table-column>
  </el-table>
  <br>
<!-- 分页 -->
  <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[10, 20, 30, 40]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
</template>
<style scoped></style>